package com.cta.entity;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import com.cta.constant.ProfileType;
import com.cta.constant.ProfileUserMapping;
import com.cta.utils.DateUtil;

@Entity
@Table(name = "cta_profile")
public class Profile implements BaseEntity {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "cta_profile_seq")
	@SequenceGenerator(name = "cta_profile_seq", sequenceName = "cta_profile_seq")
	@Column(name = "id", unique = true, nullable = false)
	private Long id;

	@Column(name = "sequence")
	private Integer sequence;

	@Column(name = "name")
	private String name;

	@Column(name = "description")
	private String description;

	@Column(name = "num_of_bytes")
	private Integer numOfBytes;

	@Column(name = "append_space")
	private Integer space;

	@Column(name = "column_name")
	private String column;

	@Enumerated(EnumType.STRING)
	@Column(name = "column_mapping")
	private ProfileUserMapping columnMapping;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "pbx_id", nullable = false)
	private PBX pbx;

	@Enumerated(EnumType.STRING)
	@Column(name = "profile_type", nullable = false)
	private ProfileType profileType;

	public Profile() {
	}

	public Profile(Integer sequence, String column, String name, String description, Integer numOfbyte, Integer space,
			ProfileType profileType, PBX pbx, ProfileUserMapping columnMapping) {
		this.sequence = sequence;
		this.column = column;
		this.name = name;
		this.description = description;
		this.numOfBytes = numOfbyte;
		this.space = space;
		this.pbx = pbx;
		this.profileType = profileType;
		this.columnMapping = columnMapping;
	}

	public Profile(Integer sequence, String column, String name, String description, Integer numOfbyte,
			ProfileType profileType, PBX pbx) {
		this.sequence = sequence;
		this.column = column;
		this.name = name;
		this.description = description;
		this.numOfBytes = numOfbyte;
		this.pbx = pbx;
		this.profileType = profileType;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Integer getNumOfBytes() {
		return numOfBytes;
	}

	public void setNumOfBytes(Integer numOfBytes) {
		this.numOfBytes = numOfBytes;
	}

	public Integer getSequence() {
		return sequence;
	}

	public void setSequence(Integer sequence) {
		this.sequence = sequence;
	}

	public PBX getPbx() {
		return pbx;
	}

	public void setPbx(PBX pbx) {
		this.pbx = pbx;
	}

	public String getColumn() {
		return column;
	}

	public void setColumn(String column) {
		this.column = column;
	}

	public ProfileType getProfileType() {
		return profileType;
	}

	public void setProfileType(ProfileType profileType) {
		this.profileType = profileType;
	}

	public Object convertData(String val) throws ParseException {
		if (this.profileType.equals(ProfileType.GENERAL) && this.column.equals("dateformat")) {
			SimpleDateFormat format = DateUtil.getFormat(this.name);
			return format.parse(val.trim());
		} else {
			return val.trim();
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Profile other = (Profile) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	public ProfileUserMapping getColumnMapping() {
		return columnMapping;
	}

	public void setColumnMapping(ProfileUserMapping columnMapping) {
		this.columnMapping = columnMapping;
	}

	public Integer getSpace() {
		return space;
	}

	public void setSpace(Integer space) {
		this.space = space;
	}
}
